package cn.mxb.logic.action;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import org.springframework.beans.factory.annotation.Autowired;

import com.opensymphony.xwork2.ModelDriven;
import com.opensymphony.xwork2.Preparable;

import cn.mxb.floor.pagination.Result;
import cn.mxb.floor.web.BaseAction;
import cn.mxb.logic.entity.SysCount;
import cn.mxb.logic.entity.SysTown;
import cn.mxb.logic.entity.SysUser;
import cn.mxb.logic.entity.SysRole;
import cn.mxb.logic.entity.SysVillage;
import cn.mxb.logic.service.IRoleDoService;
import cn.mxb.logic.service.ISysCountService;
import cn.mxb.logic.service.ISysFamilyService;
import cn.mxb.logic.service.ISysTownService;
import cn.mxb.logic.service.ISysRoleService;
import cn.mxb.logic.service.ISysVillageService;

@SuppressWarnings({ "serial", "rawtypes" })
public class SysRoleAction extends BaseAction implements Preparable,ModelDriven {
	protected static final String List_JSP = "/WEB-INF/page/web/role/roleList.jsp";
	protected static final String ADD_JSP = "/WEB-INF/page/web/role/roleAdd.jsp";
	protected static final String UPDATE_JSP = "/WEB-INF/page/web/role/roleUpdate.jsp";
	protected static final String ROLEAUTHORIZATIONLISTJSP = "/WEB-INF/page/web/role/roleauthorizationList.jsp";
	
	private SysRole sysRole;
	@Autowired
	private ISysRoleService sysRoleService;
	
	@Autowired
	private ISysCountService sysCountService;
	@Autowired
	private ISysTownService sysTownService;
	@Autowired
	private ISysVillageService sysVillageService;
	@Autowired
	private ISysFamilyService sysfamilyService;
	@Autowired
	private IRoleDoService roleDoService;
	
	private Result<SysRole> pageResult;
	
	private List<SysRole> sysRoleAllList; 
	private List<SysCount> sysCountAllList; 
	private List<SysTown> sysTownAllList; 
	private List<SysVillage> sysVillageAllList; 
	
	private Map<Integer,String> countNameMap = new HashMap<Integer, String>();  //县名
	private Map<Integer,String> townNameMap = new HashMap<Integer, String>();  //镇名
	private Map<Integer,String> villlageNameMap = new HashMap<Integer, String>();  //村名
	
	private Map<String,String> familyNameMap = new HashMap<String, String>();
	private Map<Integer,String> authorizationMap= new HashMap<Integer, String>();
	/**
	 * @return_type: String
	 * @file_name: 跳转添加界面
	 */
	public String openAdd(){
		sysCountAllList = sysCountService.findAllList();
		sysTownAllList = sysTownService.findAllList();
		sysVillageAllList = sysVillageService.findAllList();
		if(sysRole.getRoleId() !=null && !"".equals(sysRole.getRoleId())){
			setForwardView(ADD_JSP);
		}
		setForwardView(ADD_JSP);
		return SUCCESS;
	}
	
	/**
	 * 添加
	 * @return_type: String
	 */
	public String add(){
		sysRoleService.add(sysRole);
		return openList();		
	}
	/**
	 * 查看拥有权限
	 * @return
	 */
	public String havaDo(){
		sysRole = sysRoleService.findByProperty("roleId", sysRole.getRoleId());
		return action;
		
	}
	
	/**
	 * @file_name 打开列表
	 * @return 
	 */
	public String openList(){
		try{
			if(sysRole.getRoleId()  !=null && !"".equals(sysRole.getRoleId())){
				setForwardView(ROLEAUTHORIZATIONLISTJSP);
			}else{
				setForwardView(List_JSP);
			}
			pageResult = sysRoleService.find(sysRole, getPage(), getRow());
			for(SysRole sysRole : pageResult.getData()){
				if(sysRole.getRoleId()  !=null && !"".equals(sysRole.getRoleId()) && sysRole.getDoId()  !=null && !"".equals(sysRole.getDoId())){
					authorizationMap.put(sysRole.getDoId(), roleDoService.findByProperty("doId", sysRole.getDoId()+"").getDoName());
					System.out.println("authorizationMap"+authorizationMap.get(sysRole.getDoId()));
				}
				
			}
			return SUCCESS;
		}catch(Exception e){
			System.out.println(e);
		}
		return SUCCESS;
	}
	
	/**
	 * @file_name:删除 
	 */
	public String delete(){
		sysRoleService.delete(sysRole.getId());
		return openList();
	}
	
	/**
	 * @return_type: 
	 * @file_name:  打开修改页面
	 */
	public String openUpdate(){
		sysRole =  sysRoleService.findByProperty("id",sysRole.getId()+"");
		sysCountAllList = sysCountService.findAllList();
		sysTownAllList = sysTownService.findAllList();
		setForwardView(UPDATE_JSP);
		return SUCCESS;
		
	}
	
	/**
	 * 修改
	 * @return_type: 
	 */
	public String update(){
		sysRoleService.add(sysRole);
		sysRole = null;
		return openList();
	}
	
	
	@Override
	public Object getModel() {
		return sysRole;
	}
	@Override
	public void prepare() throws Exception {
		if(sysRole ==null){
			sysRole = new SysRole();
		}
	}


	public SysRole getSysRole() {
		return sysRole;
	}

	public void setSysRole(SysRole sysRole) {
		this.sysRole = sysRole;
	}

	public Result<SysRole> getPageResult() {
		return pageResult;
	}

	public void setPageResult(Result<SysRole> pageResult) {
		this.pageResult = pageResult;
	}

	public List<SysRole> getSysRoleAllList() {
		return sysRoleAllList;
	}

	public void setSysRoleAllList(List<SysRole> sysRoleAllList) {
		this.sysRoleAllList = sysRoleAllList;
	}

	public List<SysCount> getSysCountAllList() {
		return sysCountAllList;
	}

	public void setSysCountAllList(List<SysCount> sysCountAllList) {
		this.sysCountAllList = sysCountAllList;
	}

	public List<SysTown> getSysTownAllList() {
		return sysTownAllList;
	}

	public void setSysTownAllList(List<SysTown> sysTownAllList) {
		this.sysTownAllList = sysTownAllList;
	}

	public List<SysVillage> getSysVillageAllList() {
		return sysVillageAllList;
	}

	public void setSysVillageAllList(List<SysVillage> sysVillageAllList) {
		this.sysVillageAllList = sysVillageAllList;
	}

	public Map<Integer, String> getCountNameMap() {
		return countNameMap;
	}

	public void setCountNameMap(Map<Integer, String> countNameMap) {
		this.countNameMap = countNameMap;
	}

	public Map<Integer, String> getTownNameMap() {
		return townNameMap;
	}

	public void setTownNameMap(Map<Integer, String> townNameMap) {
		this.townNameMap = townNameMap;
	}

	public Map<Integer, String> getVilllageNameMap() {
		return villlageNameMap;
	}

	public void setVilllageNameMap(Map<Integer, String> villlageNameMap) {
		this.villlageNameMap = villlageNameMap;
	}

	public Map<String, String> getFamilyNameMap() {
		return familyNameMap;
	}

	public void setFamilyNameMap(Map<String, String> familyNameMap) {
		this.familyNameMap = familyNameMap;
	}

	public Map<Integer, String> getAuthorizationMap() {
		return authorizationMap;
	}

	public void setAuthorizationMap(Map<Integer, String> authorizationMap) {
		this.authorizationMap = authorizationMap;
	}

	

	

	
	

	
	
}
